1
|
|
|
import { Inject } from '@nestjs/common'; |
2
|
|
|
import { CommandHandler } from '@nestjs/cqrs'; |
3
|
|
|
import { CreateLeaveRequestCommand } from './CreateLeaveRequestCommand'; |
4
|
|
|
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository'; |
5
|
|
|
import { LeaveRequest } from 'src/Domain/HumanResource/Leave/LeaveRequest.entity'; |
6
|
|
|
import { DoesLeaveRequestExistForPeriod } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestExistForPeriod'; |
7
|
|
|
import { LeaveRequestAlreadyExistForThisPeriodException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestAlreadyExistForThisPeriodException'; |
8
|
|
|
import { DoesLeaveExistForPeriod } from 'src/Domain/FairCalendar/Specification/DoesLeaveExistForPeriod'; |
9
|
|
|
import { EventsOrLeavesAlreadyExistForThisPeriodException } from 'src/Domain/FairCalendar/Exception/EventsOrLeavesAlreadyExistForThisPeriodException'; |
10
|
|
|
import { ICommandBus } from 'src/Application/ICommandBus'; |
11
|
|
|
import { CreateNotificationCommand } from 'src/Application/Notification/Command/CreateNotificationCommand'; |
12
|
|
|
import { NotificationType } from 'src/Domain/Notification/Notification.entity'; |
13
|
|
|
import { ITranslator } from 'src/Infrastructure/Translations/ITranslator'; |
14
|
|
|
import { ConfigService } from '@nestjs/config'; |
15
|
|
|
|
16
|
|
|
@CommandHandler(CreateLeaveRequestCommand) |
17
|
|
|
export class CreateLeaveRequestCommandHandler { |
18
|
|
|
constructor( |
19
|
|
|
@Inject('ILeaveRequestRepository') |
20
|
|
|
private readonly leaveRequestRepository: ILeaveRequestRepository, |
21
|
|
|
@Inject('ICommandBus') |
22
|
|
|
private readonly commandBus: ICommandBus, |
23
|
|
|
private readonly doesLeaveRequestExistForPeriod: DoesLeaveRequestExistForPeriod, |
24
|
|
|
private readonly doesLeaveExistForPeriod: DoesLeaveExistForPeriod, |
25
|
|
|
@Inject('ITranslator') |
26
|
|
|
private readonly translator: ITranslator, |
27
|
|
|
private readonly configService: ConfigService |
28
|
|
|
) {} |
29
|
|
|
|
30
|
|
|
public async execute(command: CreateLeaveRequestCommand): Promise<string> { |
31
|
|
|
const { |
32
|
|
|
user, |
33
|
|
|
endDate, |
34
|
|
|
endsAllDay, |
35
|
|
|
type, |
36
|
|
|
startDate, |
37
|
|
|
startsAllDay, |
38
|
|
|
comment |
39
|
|
|
} = command; |
40
|
|
|
|
41
|
|
|
if ( |
42
|
|
|
true === |
43
|
|
|
(await this.doesLeaveRequestExistForPeriod.isSatisfiedBy( |
44
|
|
|
user, |
45
|
|
|
startDate, |
46
|
|
|
endDate |
47
|
|
|
)) |
48
|
|
|
) { |
49
|
|
|
throw new LeaveRequestAlreadyExistForThisPeriodException(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ( |
53
|
|
|
true === |
54
|
|
|
(await this.doesLeaveExistForPeriod.isSatisfiedBy( |
55
|
|
|
user, |
56
|
|
|
startDate, |
57
|
|
|
endDate |
58
|
|
|
)) |
59
|
|
|
) { |
60
|
|
|
throw new EventsOrLeavesAlreadyExistForThisPeriodException(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
const leaveRequest = await this.leaveRequestRepository.save( |
64
|
|
|
new LeaveRequest( |
65
|
|
|
user, |
66
|
|
|
type, |
67
|
|
|
startDate, |
68
|
|
|
startsAllDay, |
69
|
|
|
endDate, |
70
|
|
|
endsAllDay, |
71
|
|
|
comment |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
this.commandBus.execute( |
76
|
|
|
new CreateNotificationCommand( |
77
|
|
|
NotificationType.POST, |
78
|
|
|
this.translator.translate( |
79
|
|
|
'leave-requests-create-notification-message', |
80
|
|
|
{ |
81
|
|
|
userFirstName: leaveRequest.getUser().getFirstName(), |
82
|
|
|
link: `${this.configService.get<string>( |
83
|
|
|
'PERMACOOP_BASE_URL' |
84
|
|
|
)}/app/people/leaves/${leaveRequest.getId()}` |
85
|
|
|
} |
86
|
|
|
), |
87
|
|
|
leaveRequest |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
return leaveRequest.getId(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|